package com.dragon.logical.service.auth.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.logical.service.auth.AdminRoleService;
import com.dragon.repository.entity.AdminRole;
import com.dragon.repository.mapper.AdminRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 用户角色表 服务实现类
 * </p>
 *
 * @author sy
 * @since 2023-06-12
 */
@Service
public class AdminRoleServiceImpl extends ServiceImpl<AdminRoleMapper, AdminRole> implements AdminRoleService {

    @Autowired
    AdminRoleMapper arMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveAdminRole(Integer adminId, List<Integer> roleIds) {
        // 首先清除该用户原有的角色信息
        QueryWrapper<AdminRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(AdminRole::getAdminId, adminId);
        arMapper.delete(queryWrapper);
        // 再进行添加
        for (Integer roleId : roleIds) {
            AdminRole adminRole = new AdminRole();
            adminRole.setAdminId(adminId);
            adminRole.setRoleId(roleId);
            adminRole.setUpdateTime(Timestamp.valueOf(LocalDateTime.now()));
            arMapper.insert(adminRole);
        }

        return true;
    }
}
